package com.net.xpay.common.validator.admin.rbac;

import com.net.xpay.common.domain.admin.rbac.AdminUser;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.common.exception.BusinessException;
import com.net.common.util.PhoneUtil;
import com.net.xpay.common.manager.admin.rbac.AdminUserManager;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 2019/6/5 15:05

 */
@Component
public class AdminUserValidator {

    @Autowired
    private AdminUserManager adminUserManager;

    public void validateCreateAdminUser(AdminUser adminUser) {
        if (Strings.isEmpty(adminUser.getUsername())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "用户名不能为空");
        }
        if (!PhoneUtil.isEffectiveMobile(adminUser.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号不能为空");
        }
        AdminUser selected = adminUserManager.findByMobile(adminUser.getMobile());
        if (selected != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号已被其它账号使用");
        }
    }
}
